<?php
class WbSiteController extends DomController{

	public function page( $pageId ) {
		
		$item = DomAr::load( $pageId, 'WbSite');
		
		if( $_GET['deleteBanner'] && $banner = WbSiteBanner::load( $_GET['deleteBanner']) ){
			$banner->delete();
		}
		
		if( $_GET['deleteDocument'] && $document = WbSiteDocument::load( $_GET['deleteDocument']) ){
			$document->delete();
		}
		
		$this->loadController('Wb')->_updatePage( $item );
		
		$post = DomFgPost::getInstance();
		if( $post->save ) {
			
			$item->bannerLocations = $post->bannerLocations;
			$item->title = $post->title;
			
			if( $item->save() ) {
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
		
		
		$tabPanel = array(
			'type' => 'tabPanel',
			'action' => $this->getUrl('site/'.$pageId),
			'headerTabs' => true,
			'border' => true,
			'frame' => true,
			'action' => $this->getUrl('page/'.$pageId.'/'.$parentId),
			'items' => array($message),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		
		// -------------------------------------------
		
		$overallTab = array(
			'title' => 'Veebileht',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$this->loadController('Wb')->_getPageItems( $item ),
				array(
					'type' => 'textArea',
					'name' => 'bannerLocations',
					'value' => $item->bannerLocationsToString(),
					'title' => 'Bännerite asukohad',
					'style' => 'width:95%'
				),
				array(
					'type' => 'textField',
					'name' => 'title',
					'value' => $item->title,
					'title' => 'Lehe nimetus',
				),
			),
		);
		$tabPanel['items'][] = &$overallTab;
		
		// -------------------------------------------
		
		$bannersTab = array(
			'id' => 'banners',
			'title' => 'Bännerid',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150, ),
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('banner/'.$pageId),
					'colspan' => 2,
				),
				array(
					'type' => 'grid',
					'source' => new DomArDataSource( $item->banners->query()->orderBy('t.name DESC'), 40 ),
					'colspan' => 2,
					'fields' => array(
						array('key' => 'name', 'title' => 'Nimi'),
						array('key' => 'location', 'title' => 'Asukoht'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('banner/'.$pageId.'/'), 'title' => 'Valikud','colspan' => 2, 'width' => 60,),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('page/'.$pageId.'/&deleteBanner='), 'confirmMessage' => 'Are you sure?', 'width' => 60 ),
					),
				),
			),
		);
		$tabPanel['items'][] = &$bannersTab;
		
		// -------------------------------------------	
		
		$documentsTab = array(
			'id' => 'documents',
			'title' => 'Dokumendid',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150, ),
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('document/'.$pageId),
					'colspan' => 2,
				),
				array(
					'type' => 'grid',
					'source' => new DomArDataSource( $item->documents->query()->orderBy('t.name DESC'), 40 ),
					'colspan' => 2,
					'fields' => array(
						array('key' => 'name', 'title' => 'Nimi'),
						array('key' => 'key', 'title' => 'Võti'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('document/'.$pageId.'/'), 'title' => 'Valikud','colspan' => 2, 'width' => 60,),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('page/'.$pageId.'/&deleteDocument='), 'confirmMessage' => 'Are you sure?', 'width' => 60 ),
					),
				),
			),
		);
		$tabPanel['items'][] = &$documentsTab;
		
		// -------------------------------------------	
		
		$tabPanel['items'][] = $this->loadController('Wb')->_getPagePanels( $item );

		// -------------------------------------------
		$this->view->contents = DomFg::render( $tabPanel );
		$this->loadController('Wb')->_renderSiteTree( $item->getRoot(), $item );
		
	
	}
	
	public function banner( $pageId, $id = NULL ) {
		$new = $id == 0;
		$item = $new ? new WbSiteBanner() : WbSiteBanner::load($id);
		if( $new == true ) {
			$item->parent = WbSite::load( $pageId );
		}
		
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->name = $post->name;
			$item->location = $post->location;
			$item->banner->addUploaded( $post->banner[0], $post->banner[1] );
			$item->link  = $post->link;
			$item->active = (bool)$post->active;
			$item->code = $post->code;
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		$panel = array(
			'title' => 'Bänner',
			'layout' => 'table',
			'action' => $this->getUrl('banner/'.$pageId.'/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$message ,
				($item->banner->exists() ? array(
					'type' => 'image',
					'src' => $item->banner->getUrl(),
					'style' => 'width:95%; height:100px;',
					'colspan' => 2,
				) : NULL ),
				array(
					'type' => 'textField',
					'title' => 'Nimi',
					'value' => $item->name,
					'name' => 'name',
				),
				array(
					'type' => 'checkBox',
					'title' => 'Aktiivne',
					'checked' => $item->active,
					'name' => 'active',
				),
				array(
					'type' => 'select',
					'title' => 'Asukoht',
					'values' => $item->parent->bannerLocations,
					'selected' => $item->location,
					'name' => 'location',
				),
				array(
					'type' => 'file',
					'title' => 'Bänneri fail',
					'name' => 'banner',
				),
				array(
					'type' => 'textField',
					'title' => 'Link',
					'value' => $item->link,
					'name' => 'link',
				),
				array(
					'type' => 'textArea',
					'title' => 'Kood',
					'value' => $item->code,
					'name' => 'code',
				),
				
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
				array(
					'title' => 'Tagasi',
					'link' => $this->getUrl('page/'.$pageId.'&tab=banners'),
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
		$this->loadController('Wb')->_renderSiteTree( $item->parent->getRoot(), $item->parent );
	}
	
	public function document( $pageId, $id = NULL ) {
		$new = $id == 0;
		$item = $new ? new WbSiteDocument() : WbSiteDocument::load($id);
		if( $new == true ) {
			$item->parent = WbSite::load( $pageId );
		}
		
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->name = $post->name;
			$item->key = $post->key;
			$item->file->addUploaded( $post->file[0], $post->file[1] );
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 3,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' =>3,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		$panel = array(
			'title' => 'Dokument',
			'layout' => 'table',
			'action' => $this->getUrl('document/'.$pageId.'/'.$id),
			'layoutConfig' => array(
				'columns' => 3,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$message ,
				($item->key ? array(
					'type' => 'info',
					'message' => 'Dokumendi kasutamiseks kirjutage teksti sisse <b>#'.$item->key.'</b>',
					'colspan' => 3,
				) : NULL),
				array(
					'type' => 'textField',
					'title' => 'Nimi',
					'value' => $item->name,
					'name' => 'name',
					'colspan' => 2,
				),
				array(
					'type' => 'textField',
					'title' => 'Võti',
					'value' => $item->key,
					'name' => 'key',
				),
				array(
					'type' => 'tip',
					'message' => 'Võtit kasutatakse dokumendi teksti sisestamiseks.',
				),
				array(
					'type' => 'file',
					'title' => 'Dokumendi fail',
					'name' => 'file',
					'colspan' => 2,
				),
				( !$item->file->exists() ? NULL : array(
					'type' => 'value',
					'title' => 'Olemasolev fail',
					'value' => '<a href="'.$item->file->getUrl().'" target="_blank">'.$item->file->getName().'.'.$item->file->getExt().'</a>',
					'colspan' => 3,
				) ),
				
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
				array(
					'title' => 'Tagasi',
					'link' => $this->getUrl('page/'.$pageId.'&tab=documents'),
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
		$this->loadController('Wb')->_renderSiteTree( $item->parent->getRoot(), $item->parent );
	}
}